<script setup>
import { ref, onMounted, onUnmounted } from "vue";

const visible = ref(false);
const scrollThreshold = 300; // 滚动多少像素后显示按钮

const handleScroll = () => {
  visible.value = window.scrollY > scrollThreshold;
};

const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: "smooth",
  });
};

onMounted(() => {
  window.addEventListener("scroll", handleScroll);
});

onUnmounted(() => {
  window.removeEventListener("scroll", handleScroll);
});
</script>

<template>
  <div class="back-to-top" v-show="visible" @click="scrollToTop">
    <el-icon :size="20"><ArrowUp /></el-icon>
  </div>
</template>

<style scoped>
.back-to-top {
  position: fixed;
  right: 40px;
  bottom: 40px;
  width: 40px;
  height: 40px;
  background-color: var(--primary-color);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
  transition: all 0.3s;
  z-index: 99;
}

.back-to-top:hover {
  background-color: var(--primary-color-dark);
  transform: translateY(-3px);
}

@media (max-width: 768px) {
  .back-to-top {
    right: 20px;
    bottom: 20px;
    width: 36px;
    height: 36px;
  }
}
</style> 